﻿/*  Francis Nino Seisei 
 *  ©2012 Francis Antonio Bolaños Arias.
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Seisei.Nino.Reader
{
    public class Column
    {
        private string _Name;
        private string _DataType;
        private int _DataTypeNumber;
        private int _DataLength = -1;
        private int _DataScale = -1;
        private int _DataPrecision = -1;
        private bool _Nullable;
        private bool _Identity;
        private bool _Primary;
        private bool _Foreign;
        private string _ForeignTable;

        public Column()
        {
            this.Name = "";
            this.DataType = "";
            this.Nullable = false;
            this.Identity = false;
            this.Primary = false;
            this.Foreign = false;
            this.ForeignTable = "";
        } 

        public Column(string Name, string DataType, bool Nullable, bool Identity, bool Primary, bool Foreign, string ForeignTable) {
            this.Name = Name;
            this.DataType = DataType;
            this.Nullable = Nullable;
            this.Identity = Identity;
            this.Primary = Primary;
            this.Foreign = Foreign;
            this.ForeignTable = ForeignTable;
        }

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                if (value.Contains(" ")) {
                    value = value.Replace(" ", "_");
                }
                _Name = value;
            }
        }

        public string DataType
        {
            get
            {
                return _DataType;
            }
            set
            {
                string DT = "Undefined";
                string Size = "";
                try
                {
                    Size = value.Substring(value.IndexOf('('));
                }
                catch (ArgumentOutOfRangeException) { }
                if (!string.IsNullOrEmpty(Size))
                {
                    DT = value.Replace(Size, "");
                    Size = Size.Replace("(", "").Replace(")", "");
                    string[] aux = Size.Split(',');
                    if (aux.Length == 2)
                    {
                        this._DataScale = int.Parse(aux[0].Trim());
                        this._DataPrecision = int.Parse(aux[1].Trim());
                    }
                    else {
                        this._DataLength = int.Parse(aux[0].Trim());
                    }
                }
                else
                {
                    DT = value;
                }
                _DataTypeNumber = Global.DataTypes.GetDataTypeNumber(DT);
                _DataType = value;
            }
        }

        public int DataTypeNumber
        {
            get
            {
                return _DataTypeNumber;
            }
        }

        public int DataLength
        {
            get
            {
                return _DataLength;
            }
        }

        public int DataScale
        {
            get
            {
                return _DataScale;
            }
        }

        public int DataPrecision
        {
            get
            {
                return _DataPrecision;
            }
        }

        public bool Nullable
        {
            get
            {
                return _Nullable;
            }
            set
            {
                //Validar
                _Nullable = value;
            }
        }

        public bool Identity
        {
            get
            {
                return _Identity;
            }
            set
            {
                //Validar
                _Identity = value;
            }
        }

        public bool Primary
        {
            get
            {
                return _Primary;
            }
            set
            {
                //Validar
                _Primary = value;
            }
        }

        public bool Foreign
        {
            get
            {
                return _Foreign;
            }
            set
            {
                //Validar
                _Foreign = value;
            }
        }

        public string ForeignTable
        {
            get
            {
                return _ForeignTable;
            }
            set
            {
                //Validar
                _ForeignTable = value;
            }
        }
    }
}
